// 项目union key
export function projectKey() {
  return process.env.VUE_APP_KEY
}

// 项目名称
export function projectName() {
  return window.$projectName
}

// 状态显示
export function isVisibleStatus(mainKey, code) {
  const point = process.env.VUE_APP_OPERATION_VISIBLE
  if(!point) return false
  if(point[mainKey]) {
    return point[mainKey][code]
  }
  return true
}

/**
 * 项目路由过滤
 * @param {*} type 路由的类型,webRouter / backStackRoutes
 * @param {*} originalRouter 原路由数组
 */
export function filterProjectRouter(type, originalRouter) {
  const processRouter = process.env.VUE_APP_USABLE_ROUTER_LIST
  if(!processRouter || !processRouter[type]) return originalRouter  // 如果没有就默认返回全部路由
  const canUserRouter = Object.values(processRouter[type]) // 项目可用路由
  let resultRouterList = []
  canUserRouter.forEach(item => {
    if(typeof item === 'object') {
       const { relatedId } = item
       const tempT = originalRouter.filter(t => t.relatedId === relatedId)
       tempT.forEach(i => {
        if(item[i.name]) {
          Object.assign(i, item[i.name])
        }
        resultRouterList.push(i)
       })
    } else {
      const targets = originalRouter.filter(t => item === t.relatedId)
      resultRouterList.push(...targets)
    }
  })
  return resultRouterList
}

// 后台侧边栏的过滤
export function backstageMenuTreeFilter(allRouter) {
  if(!process.env.VUE_APP_USABLE_ROUTER_LIST) return []
  const processRouter = process.env.VUE_APP_USABLE_ROUTER_LIST.backstageRouterTree
  if(!processRouter) return []
  const list = Object.values(processRouter)
  return list.reduce((pre, cur) => {
    const { rootName, childRelatedId } = cur
    // 收集子节点的路由配置
    const child = Object.values(childRelatedId).reduce((pre, cur) => {
      const t = allRouter.find(t => cur.relatedId === t.relatedId)
      if(t) {
        if(cur.alias) { // 别名替换
          t.name = cur.alias
        }
        pre.push(t)
      }
      return pre
    }, [])
    pre.push({ title: rootName, child })
    return pre
  }, [])
}
